from flask import Blueprint, request
from utilsapp.mysql_con import db_operation, db_connect, db_fetchone
sensorType = Blueprint('sensorType', __name__)
from utilsapp.mysql_con import db_delete, db_insert, db_fetchone, db_update, db_pagelist, db_table_list,db_table_pagelist


# 传感器类型——查询
@sensorType.route('/getlist', methods=["post"])
def sensor_type_list():
    if 'page' in request.json.keys() and 'size' in request.json.keys():
        page = request.json['page']
        size = request.json['size']
    else:
        page = 1
        size = 999
    j = db_table_pagelist('sensor_type', page, '', size)
    return j


# 传感器类型——增加
@sensorType.route('/add', methods=['POST'])
def sensor_type_add():
    return db_insert('sensor_type', request.json)


# 传感器类型——删除
@sensorType.route('/delete', methods=['POST'])
def sensor_type_delete():
    id = request.json['id']
    return db_delete('sensor_type', f'WHERE id=%s', {'id': id})


# 传感器类型——修改
@sensorType.route('/edit', methods=['POST'])
def sensor_type_edit():
    id = request.json['id']
    keys_not_add = ['id']
    addModel = {key: request.json[key]
                for key in request.json if key not in keys_not_add}
    return db_update("sensor_type", addModel, f'WHERE id=%s', {"id": id})


@sensorType.route('/get/eig', methods=['POST'])
def get_eig():
    sensor_type = request.json['sensor_type']
    sql = f"select eigenvalue from sensor_type where id = {sensor_type}"
    return db_fetchone(sql)
